package com.sskj.xgja.login;


import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.hjq.toast.ToastUtils;
import com.sskj.xgja.MVPActivity;
import com.sskj.base.utils.CheckUtil;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.EditUtil;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.R;
import com.sskj.xgja.WebViewActivity;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/03/21
 */
public class RegisterActivity extends MVPActivity<RegisterPresenter> {
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.account)
    EditText account;
    @BindView(R.id.verify_code)
    EditText verifyCode;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.register_ps)
    EditText registerPs;
    @BindView(R.id.register_ps2)
    EditText registerPs2;
    @BindView(R.id.invite_code)
    EditText inviteCode;
    @BindView(R.id.etName)
    EditText etName;
    @BindView(R.id.register_btn)
    Button registerBtn;
    @BindView(R.id.ps_lock)
    ImageView psLock;
    @BindView(R.id.ps2_lock)
    ImageView ps2Lock;
    @BindView(R.id.check_register_user_agreement)
    CheckBox mCheckRegisterUserAgreement;
    @BindView(R.id.register_user_agreement)
    TextView mRegisterUserAgreement;

    @Override
    public int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public RegisterPresenter getPresenter() {
        return new RegisterPresenter();
    }

    @Override
    public void initView() {
        toolbar.setLeftButtonOnClickListener(v -> finish());
        psLock.setOnClickListener(v -> {
            if (EditUtil.togglePs(registerPs)) {
                psLock.setImageResource(R.mipmap.icon_lock);
            } else {
                psLock.setImageResource(R.mipmap.icon_unlock);
            }
        });
        ps2Lock.setOnClickListener(v -> {
            if (EditUtil.togglePs(registerPs2)) {
                ps2Lock.setImageResource(R.mipmap.icon_lock);
            } else {
                ps2Lock.setImageResource(R.mipmap.icon_unlock);
            }
        });

    }

    @Override
    public void initData() {
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        ClickUtil.click(mRegisterUserAgreement, view -> WebViewAgressActivity.start(this, "reg_agree"));
        //登录
        ClickUtil.click(toolbar.getRightButton(), v -> LoginActivity.start(this));
        //发送验证码
        ClickUtil.click(getCode, view -> {
            if (isEmpty(account)) {
                ToastUtils.show("请输入资金账号");
                return;
            }
            if (!CheckUtil.isMobile(getText(account))) {
                ToastUtils.show("账号格式不正确");
                return;
            }
            mPresenter.sendCode(getText(account));
        });
        //注册
        ClickUtil.click(registerBtn, v -> {
            if (isEmpty(etName)) {
                ToastUtils.show("请输入姓名");
                return;
            }
            if (isEmpty(account)) {
                ToastUtils.show("请输入资金账号");
                return;
            }
            if (!CheckUtil.isMobile(getText(account))) {
                ToastUtils.show("账号格式不正确");
                return;
            }

            if (isEmpty(verifyCode)) {
                ToastUtils.show(R.string.input_verify_code);
                return;
            }
            if (isEmpty(registerPs)) {
                ToastUtils.show(R.string.input_login_ps);
                return;
            }
            if (isEmpty(registerPs2)) {
                ToastUtils.show(R.string.input_login_ps_again);
                return;
            }

            if (!CheckUtil.isPwd(getText(registerPs))) {
                ToastUtils.show(R.string.ps_type_err);
                return;
            }

            if (!getText(registerPs).equals(getText(registerPs2))) {
                ToastUtils.show(R.string.repeat_ps_error);
                return;
            }
//            if (isEmpty(inviteCode)) {
//                ToastUtils.show(R.string.input_invite_code);
//                return;
//            }
            if (!mCheckRegisterUserAgreement.isChecked()) {
                ToastUtils.show("请先同意注册协议");
                return;
            }
            mPresenter.register(getText(etName),getText(account), getText(verifyCode), getText(registerPs), getText(registerPs2), getText(inviteCode));
        });
    }


    public static void start(Context context) {
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    public void sendCodeSuccess() {
        startTimeDown(getCode);
    }

    public void registerSuccess() {
        LoginActivity.start(this);
    }
}
